<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改物流协议')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-transAgreement-edit" th:object="${transAgreement}">
        <input name="id" th:field="*{id}" type="hidden" id="id">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">协议单号：</label>
            <div class="col-sm-8">
                <input name="agreementId" th:field="*{agreementId}" class="form-control" type="text" required readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">物流商名称：</label>
            <div class="col-sm-8">
                <input name="transId" th:field="*{transId}" class="form-control hidden" type="text">
                <input name="transName" th:field="*{transName}" class="form-control" type="text"
                       onclick="checkTransUnit()" readonly required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">协议开始日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="agreementStart"
                           th:value="${#dates.format(transAgreement.agreementStart, 'yyyy-MM-dd')}" class="form-control"
                           placeholder="yyyy-MM-dd" type="text" required>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">协议结束日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="agreementEnd" th:value="${#dates.format(transAgreement.agreementEnd, 'yyyy-MM-dd')}"
                           class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                </div>
            </div>
        </div>
        <!--<div class="form-group">
            <label class="col-sm-3 control-label">计价方式：</label>
            <div class="col-sm-8">
                <select name="priceType" class="form-control m-b" th:with="type=${@dict.getType('price_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{priceType}"></option>
                </select>
            </div>
        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">单价：</label>
            <div class="col-sm-8">
                <input name="price" th:field="*{price}" class="form-control" type="number" required min="0">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">结算方式：</label>
            <div class="col-sm-8">
                <select name="settleType" class="form-control m-b" th:with="type=${@dict.getType('settle_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{settleType}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">协议内容：</label>
            <div class="col-sm-8">
                <textarea name="agreeContent" id="agreeContent" class="form-control hidden" type="text" th:field="*{agreeContent}"></textarea>
                <div id="editor" class="summernote"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: summernote-js" />
<script type="text/javascript">
    var prefix = ctx + "bus/transAgreement";
    $("#form-transAgreement-edit").validate({
        focusCleanup: true
    });


    function submitHandler() {
        var id = $("#id").val();
        var url = "";
        if (id == "") {
            url = prefix + "/add";
        } else {
            url = prefix + "/edit";
        }
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#agreeContent").val(sHTML);
            $.operate.save(url, $('#form-transAgreement-edit').serialize());
        }
    }

    $("input[name='agreementStart']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='agreementEnd']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    //选择物流商代码页面
    function checkTransUnit() {
        $.modal.open("选择物流商信息", "/pageUtil/chooseTransUnit");
    }

    //选中物流商代码
    function checkedTransUnit(transUnit) {
        $('#transId').val(transUnit.transId);
        $('#transName').val(transUnit.transName);
    }

    $('.summernote').summernote({
        placeholder: '请输入公告内容',
        height: 192,
        lang: 'zh-CN',
        followingToolbar: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    $(function() {
        var id=$("#id").val();
        debugger
        if(id!="") {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height: 192,
                lang: 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#agreeContent").val();
            $('#editor').summernote('code', content);
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function(error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

</script>
</body>
</html>